package cn.usian.controller;

import cn.usian.pojo.User;
import cn.usian.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("findPage")
    public String findPage(Model model,@RequestParam(name = "current",required = true,defaultValue = "1")int current){
        Page<User>page=userService.findPage(current,3);
        model.addAttribute("page",page);
        return "list";
    }
    //删除
    @GetMapping("del")
    public String del(Long id){
        userService.del(id);
        return "redirect:/all";
    }
    @GetMapping("findById")
    public String findById(Long id,Model model){
        User user=userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }
    @RequestMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:/all";
    }
    @RequestMapping("toadd")
    public String toAdd(){
        return "add";
    }
    @RequestMapping("add")
    public String add(User user){
        userService.add(user);
        return "redirect:/all";
    }
    @GetMapping("findByName")
    public String findByName(String name,Model model){
        List<User> list= userService.findByName(name);
        model.addAttribute("list",list);
        return "index";
    }

}
